package com.topnav.mc.document.entity.wrapper;

import com.topnav.mc.admin.entity.param.DeviceParam;
import com.topnav.mc.base.mc.McWrapper;
import com.topnav.mc.document.entity.param.DocumentParam;
import org.springframework.util.StringUtils;

/**
 * @Author: kenny
 * @Date: 2023/11/23 14:32
 */
public class DocumentWrapper extends McWrapper<DocumentParam> {
    public DocumentWrapper(DocumentParam entity) {
        super(entity);
        if (this.permissionGrade != null ){
            if (this.permissionGrade.getPermissionLevel() == 1){
                this.eq("d.user_id",this.permissionGrade.getUserId());
            }else if (this.permissionGrade.getPermissionLevel() == 2){
                this.eq("d.org_id",this.permissionGrade.getOrgId());
            }else if (this.permissionGrade.getPermissionLevel() == 3 || this.permissionGrade.getPermissionLevel() == 4){
                this.likeRight("o.path",this.permissionGrade.getOrgPath());
            }
        }
        if (entity != null){
            if (entity.getId()!=null){this.eq("d.id",entity.getId());}
            if (entity.getDeleteFlag()!=null){this.eq("d.delete_flag",entity.getDeleteFlag());}
            if (entity.getOrgId() != null){this.eq("d.org_id",entity.getOrgId());}
            if (entity.getUserId() != null){this.eq("d.user_id",entity.getUserId());}
            if (entity.getSiteId() != null){this.eq("d.site_id",entity.getSiteId());}
            if (entity.getDeviceId() != null){this.eq("d.device_id",entity.getDeviceId());}
            if (entity.getUpdateTimeFrom() != null){this.ge("d.update_time",entity.getUpdateTimeFrom());}
            if (entity.getUpdateTimeTo() != null){this.le("d.update_time",entity.getUpdateTimeTo());}
            if (entity.getUploadTimeFrom() != null){this.ge("d.upload_time",entity.getUploadTimeFrom());}
            if (entity.getUploadTimeTo() != null){this.le("d.upload_time",entity.getUploadTimeTo());}
            if (entity.getExpireTimeFrom() != null){this.ge("d.expire_time",entity.getExpireTimeFrom());}
            if (entity.getExpireTimeTo() != null){this.le("d.expire_time",entity.getExpireTimeTo());}

            if (entity.getImportance() != null){this.eq("d.importance",entity.getImportance());}
            if (entity.getFileType() != null){this.eq("d.file_type",entity.getFileType());}

            if (!StringUtils.isEmpty(entity.getDocName())){this.like("d.doc_name",entity.getDocName());}
            if (!StringUtils.isEmpty(entity.getRemark())){this.like("d.remark",entity.getRemark());}

            if (!StringUtils.isEmpty(entity.getKeyword())){
                this.and(wrapper -> wrapper.like("d.doc_name", entity.getKeyword())
                        .or().like("d.remark", entity.getKeyword())
                );
            }
            this.orderByDesc("d.id");

        }
    }
}
